<?php
/**
 * User: aka
 * Time: 1:26
 */

namespace app\common;

use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Session;

class CommentEmail
{
    private $email;
    private $password;
    private $qqHost;
    private $port;
    private $SMTPSecure;
    private $qqAccount;
    private $emailTitle;
    private $sendName;
    private $content;
    private $receiveAddress;
    private $codeType;
    private $sendUserAccount;

    public function __construct()
    {
        $this->email = new PHPMailer();
        $this->password = 'hzvlwgkdfwljcagg';
        $this->qqHost = 'smtp.qq.com';
        $this->port = '465';
        $this->SMTPSecure = 'ssl';
        $this->qqAccount = '425084984@qq.com';
        $this->emailTitle = config('app.app_name').' - 有位用户给您邮箱留言了！';
        $this->sendName = 'LONG57';
        $this->content = null;
        $this->receiveAddress = null;
        $this->codeType = 'UTF-8';
        $this->sendUserAccount = null;
    }

    public function __destruct()
    {
        unset(
            $this->email,
            $this->password,
            $this->qqHost,
            $this->port,
            $this->SMTPSecure,
            $this->qqAccount,
            $this->emailTitle,
            $this->sendName,
            $this->content,
            $this->receiveAddress,
            $this->codeType,
            $this->sendUserAccount
        );
    }

    public function setEmailContent(string $userName, string $commentContent)
    {
        $this->content = '游戏 ID：'.'<br>'
            .$userName.'<br>'
            .'对您说：'.'<br>'
            .$commentContent.'<br>'
            .'<br>'
            .'请在游戏中通过 ID 联系对方！';
    }

    public function setReceiveAddress(string $emailAddress)
    {
        $this->receiveAddress = $emailAddress;
    }

    public function setSendUserAccount(string $emailAddress)
    {
        $this->sendUserAccount = $emailAddress;
    }

    public function send()
    {
        $this->email->isSMTP();
        $this->email->CharSet = $this->codeType;
        $this->email->Host = $this->qqHost;
        $this->email->Port = $this->port;
        $this->email->SMTPAuth = true;
        $this->email->SMTPSecure = $this->SMTPSecure;
        $this->email->Username = $this->qqAccount;
        $this->email->Password = $this->password;
        $this->email->From = $this->qqAccount;
        $this->email->FromName = $this->sendName;
        $this->email->Subject = $this->emailTitle;
        $this->email->addAddress($this->receiveAddress);
        $this->email->isHTML(true);
        $this->email->Body = $this->content;
        return $this->email->send() ?
            $this->saveEmailData()
            :
            json($this->email->ErrorInfo);
    }

    private function saveEmailData()
    {
        \app\tenno\model\Email::create([
            'receive_account' => $this->receiveAddress,
            'send_email_address' => $this->sendUserAccount,
            'content' => $this->content,
            'delete_state' => 0
        ]);
        return json(['state' => 200]);
    }
}